package Day12;

import java.util.Objects;
import java.util.TreeSet;

public class Demo07 {
    public static void main(String[] args) {
        //TreeSet   当自己定义方法用该方法存储时  需要自己定义比较规则
        TreeSet<Person> set = new TreeSet<>();
        Person p1 = new Person("张三",18);
        Person p2 = new Person("李四",19);
        Person p3 = new Person("麻子",8);
        boolean b = set.add(p1);
        boolean a = set.add(p2);
        set.add(p3);
        System.out.println(b);
        System.out.println(a);
        for (Person p:set) {
            System.out.println(p);
        }

    }
}
class Person implements Comparable<Person>{//
    private String name;
    private int age;

    @Override
    public int compareTo(Person o) {
        //return返回  负数this小/零一样大/正数this大
        if(this.age > o.age){
            return 1;
        }else if(this.age == o.age){
            return 0;
        }
        return -1;
    }

    public Person() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

}
